#!/usr/bin/env python3
# -*- coding:utf-8 -*-
from bs4 import BeautifulSoup


def parsingHTML():
    with open("resource\source.html", 'rb+') as f:
        htmlText = f.read()
    # 标准库解析HTML
    bs = BeautifulSoup(htmlText, 'html.parser')
    print(bs.title)
    print(bs.title.text)
    # 1、按属性attributes获取信息
    # for item in bs.find_all('div', {'class': {"imgpage"}}):
    #     # print(type(item))
    #     # 强制转换
    #     item_str=str(item)
    #     with open(r"resource\analyzeHtml.html", 'a+', encoding='utf-8') as f:
    #         f.write(item_str)

    # lxml HTML解析器
    soup = BeautifulSoup(htmlText, 'lxml')
    print(soup.title)
    print(soup.title.string)
    # 2、CSS选择器获取信息
    for item in soup.select('div[class="imgpage"]'):
        item_str = str(item)
        with open(r"resource\analyzeHtml.html", 'a+', encoding='utf-8') as f:
            f.write(item_str)

    # for item in soup.find_all('a'):
    #     print(item.get('href'))
    # print(soup.get_text())
    # # CSS选择器搜索
    # # 通过标签查找
    # print(soup.select('title'))
    # # 通过类名查找
    # print(soup.select('.imgpage'))
    # # 通过id名查找
    # print(soup.select('#closest'))
    # print(soup.select('#common-back-top,a'))
    # print(soup.select('div[class="undrag-text-tip"]'))


if __name__ == '__main__':
    # html解析
    parsingHTML()
    print('done')
